package com.jumi.microservice.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jumi.microservice.common.base.BaseController;
import com.jumi.microservice.common.base.dto.BasePageResponse;
import com.jumi.microservice.common.utils.ResultGenerator;
import com.jumi.microservice.constant.GoodsKeyConstant;
import com.jumi.microservice.constant.NumberConstant;
import com.jumi.microservice.domain.GoodsDO;
import com.jumi.microservice.domain.GoodsFootprintDO;
import com.jumi.microservice.domain.GoodsSpuInventoryDTO;
import com.jumi.microservice.mapper.GoodsMapper;
import com.jumi.microservice.service.GoodsFootprintService;
import com.jumi.microservice.service.GoodsInventoryApi;
import com.jumi.microservice.service.GoodsService;
import com.jumi.microservice.service.GoodsSkuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jumi.microservice.common.redis.RedisCache;

import java.math.BigDecimal;
import java.util.*;


@RestController
@Api(tags = "IM客服系统我的足迹接口")
@RequestMapping("/commodity/goods/footprint")
public class MyFootprintController extends BaseController {

    @Autowired
    private GoodsFootprintService goodsFootprintService;
    @Autowired
    private GoodsMapper goodsMapper;
    @Autowired
    private RedisCache redisCache;
    @Autowired
    private GoodsSkuService goodsSkuService;
    @DubboReference
    private GoodsInventoryApi goodsInventoryApi;
    @Autowired
    private GoodsService goodsService;


    @ApiOperation(value = "足迹列表", notes = "足迹列表")
    @GetMapping("/list")
    public BasePageResponse listFootprints(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer size,
            Long memberId) {
        LambdaQueryWrapper<GoodsFootprintDO> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(GoodsFootprintDO::getMemberId, memberId);
        wrapper.orderByDesc(GoodsFootprintDO::getBroTime);

        IPage<GoodsFootprintDO> pageInfo = goodsFootprintService.page(new Page<>(page, size), wrapper);
        //spuid集合
        List<Long> goodsSpuIds = new ArrayList<>();
        for (GoodsFootprintDO goodsFootprintDO : pageInfo.getRecords()) {
            goodsSpuIds.add(goodsFootprintDO.getGoodsId());
        }

        List<Long> goodsSkuIds = new ArrayList<>();
        if (goodsSpuIds.size() > NumberConstant.NUMBER_ZERO.intValue()) {
            goodsSkuIds = goodsService.getSkuIdListBySpuIds(goodsSpuIds);
        }

        Map<Long, List<GoodsSpuInventoryDTO>> spuInventoryMap = new HashMap<>(pageInfo.getRecords().size());
        if (goodsSkuIds.size() > NumberConstant.NUMBER_ZERO.intValue()) {
            //调用库存服务查询库存
            List<GoodsSpuInventoryDTO> goodsSpuInventories = goodsInventoryApi.goodsInventoryBySkuIds(goodsSkuIds);
            for (GoodsSpuInventoryDTO goodsSpuInventory : goodsSpuInventories) {
                if (!spuInventoryMap.containsKey(goodsSpuInventory.getSpuId())) {
                    List<GoodsSpuInventoryDTO> goodsSpuInventoryDTOList = new ArrayList<>();
                    goodsSpuInventoryDTOList.add(goodsSpuInventory);
                    spuInventoryMap.put(goodsSpuInventory.getSpuId(), goodsSpuInventoryDTOList);
                } else {
                    spuInventoryMap.get(goodsSpuInventory.getSpuId()).add(goodsSpuInventory);
                }
            }
        }

        // 判断是否开启虚拟销量
        int enableVirtualNum = redisCache.getCacheObject(GoodsKeyConstant.GOODS_VIRTUALNUM_KEY);

        for (GoodsFootprintDO goodsFootprintDO : pageInfo.getRecords()) {
            //查询商品信息
            GoodsDO goods = goodsMapper.selectById(goodsFootprintDO.getGoodsId());
            goodsFootprintDO.setGoodsName(goods.getGoodsName());

            Object cacheObject = redisCache.getCacheObject(GoodsKeyConstant.POSTAGEGOODS + goods.getId());
            if (!Objects.isNull(cacheObject)) {
                goodsFootprintDO.setGoodsPrice(BigDecimal.ZERO);
            } else {
                goodsFootprintDO.setGoodsPrice(goods.getGoodsPrice());
            }

            goodsFootprintDO.setGoodsImage(goods.getGoodsImage());
            //市场价
            goodsFootprintDO.setGoodsMarketprice(goods.getGoodsMarketprice());
            //商品描述
            goodsFootprintDO.setGoodsDescription(goods.getGoodsDescription());
            //计算销量
            if (spuInventoryMap.containsKey(goodsFootprintDO.getGoodsId())) {
                List<GoodsSpuInventoryDTO> goodsSpuInventoryDtoList = spuInventoryMap.get(goodsFootprintDO.getGoodsId());
                Integer storage = NumberConstant.NUMBER_ZERO.intValue();
                Integer saleStorage = NumberConstant.NUMBER_ZERO.intValue();
                for (GoodsSpuInventoryDTO goodsSpuInventory : goodsSpuInventoryDtoList) {
                    if (goodsSpuInventory.getSaledStorage() != null) {
                        storage += goodsSpuInventory.getSaledStorage();
                    }
                    if (goodsSpuInventory.getStockQuantity() != null) {
                        saleStorage += goodsSpuInventory.getStockQuantity();
                    }
                }
                if (enableVirtualNum == NumberConstant.NUMBER_ONE.intValue()) {
                    Integer virtualNum = goodsSkuService.countVirtualNumBySpuId(goodsFootprintDO.getGoodsId());
                    if (virtualNum != null) {
                        storage += virtualNum;
                    }
                }
                goodsFootprintDO.setSaledStorage(storage);
                goodsFootprintDO.setSaleStorage(saleStorage);
            } else {
                goodsFootprintDO.setSaledStorage(NumberConstant.NUMBER_ZERO.intValue());
                goodsFootprintDO.setSaleStorage(NumberConstant.NUMBER_ZERO.intValue());
            }
        }
        return ResultGenerator.genSuccessResultPage(pageInfo.getRecords(), pageInfo.getTotal());
    }

}
